//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BasisIncompleteOrdered.h"
using namespace Cephei::QL::Math::Matrixutilities;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::CBasisIncompleteOrdered (UInt64 euclideanDimension) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phBasisIncompleteOrdered = NULL;
#endif
        QuantLib::Size _euclideanDimension = (QuantLib::Size)ValueHelper::Convert (euclideanDimension);
        _ppBasisIncompleteOrdered = new boost::shared_ptr<QuantLib::BasisIncompleteOrdered> (new QuantLib::BasisIncompleteOrdered ( _euclideanDimension ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::CBasisIncompleteOrdered (boost::shared_ptr<QuantLib::BasisIncompleteOrdered>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBasisIncompleteOrdered = NULL;
#endif
	_ppBasisIncompleteOrdered = &childNative;
    
}
Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::CBasisIncompleteOrdered (QuantLib::BasisIncompleteOrdered& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBasisIncompleteOrdered = NULL;
#endif
	_ppBasisIncompleteOrdered = new boost::shared_ptr<QuantLib::BasisIncompleteOrdered> (&childNative);
    
    _BasisIncompleteOrderedOwner = owner;
    
}

Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::CBasisIncompleteOrdered (CBasisIncompleteOrdered^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBasisIncompleteOrdered = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBasisIncompleteOrdered = new boost::shared_ptr<QuantLib::BasisIncompleteOrdered> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::CBasisIncompleteOrdered (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBasisIncompleteOrdered = NULL;
#endif
	if (!t->IsSubclassOf(CBasisIncompleteOrdered::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::CBasisIncompleteOrdered (QuantLib::Handle<QuantLib::BasisIncompleteOrdered>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBasisIncompleteOrdered = &childNative;
	_ppBasisIncompleteOrdered = &static_cast<boost::shared_ptr<QuantLib::BasisIncompleteOrdered>>(childNative.currentLink());
    
    _BasisIncompleteOrderedOwner = owner;
}
Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::CBasisIncompleteOrdered (QuantLib::Handle<QuantLib::BasisIncompleteOrdered> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBasisIncompleteOrdered = &childNative;
	_ppBasisIncompleteOrdered = &static_cast<boost::shared_ptr<QuantLib::BasisIncompleteOrdered>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::CBasisIncompleteOrdered (QuantLib::BasisIncompleteOrdered childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBasisIncompleteOrdered = NULL;
#endif
	_ppBasisIncompleteOrdered = new boost::shared_ptr<QuantLib::BasisIncompleteOrdered> (new QuantLib::BasisIncompleteOrdered (childNative));
    
}
#endif

Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::~CBasisIncompleteOrdered ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBasisIncompleteOrdered != NULL)
    {
	    delete _ppBasisIncompleteOrdered;
        _ppBasisIncompleteOrdered = NULL;
    }
}
Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::!CBasisIncompleteOrdered ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBasisIncompleteOrdered != NULL)
    {
	    delete _ppBasisIncompleteOrdered;
    }
}
QuantLib::BasisIncompleteOrdered& Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::GetReference ()
{
    if (_ppBasisIncompleteOrdered == NULL) throw gcnew NativeNullException ();
	return **_ppBasisIncompleteOrdered;
}
boost::shared_ptr<QuantLib::BasisIncompleteOrdered>& Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::GetShared ()
{
    if (_ppBasisIncompleteOrdered == NULL) throw gcnew NativeNullException ();
	return *_ppBasisIncompleteOrdered;
}
QuantLib::BasisIncompleteOrdered* Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::GetPointer ()
{
    if (_ppBasisIncompleteOrdered == NULL) throw gcnew NativeNullException ();
	return &**_ppBasisIncompleteOrdered;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BasisIncompleteOrdered>& Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::GetHandle ()
{
	if (_phBasisIncompleteOrdered == NULL)
	{
		_phBasisIncompleteOrdered = new Handle<QuantLib::BasisIncompleteOrdered> (*_ppBasisIncompleteOrdered);
	}
	return *_phBasisIncompleteOrdered;
}
#endif
bool Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::HasNative () 
{
	return (_ppBasisIncompleteOrdered != NULL);
}

Boolean Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::AddVector (Cephei::QL::Math::IArray^ newVector)
{
    CArray^ _CnewVector;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CnewVector = safe_cast<CArray^> (newVector);
        _CnewVector->Lock();
        QuantLib::Array& _newVector = static_cast<QuantLib::Array&> (_CnewVector->GetReference ()); 
    	bool _rv = (bool)(*_ppBasisIncompleteOrdered)->addVector ( _newVector );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CnewVector != nullptr) _CnewVector->Unlock();
    }
}
UInt64 Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::BasisSize::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppBasisIncompleteOrdered)->basisSize ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::EuclideanDimension::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppBasisIncompleteOrdered)->euclideanDimension ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered::GetBasisAsRowsInMatrix::get ()
{
    try
    {
    	QuantLib::Matrix _rv = (QuantLib::Matrix)(*_ppBasisIncompleteOrdered)->getBasisAsRowsInMatrix ( );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Matrixutilities::IBasisIncompleteOrdered^ Cephei::QL::Math::Matrixutilities::CBasisIncompleteOrdered_Factory::Create (UInt64 euclideanDimension)
{
    return gcnew CBasisIncompleteOrdered ( euclideanDimension);
}
